<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-18
 * Time: 23:06
 */

namespace app\admin\controller;


use app\base\BaseController;
use app\base\model\User;
use think\Loader;
use think\Request;

class Usermanger extends BaseController
{
    /***
     * 输出用户管理页视图
     * @return mixed
     */
    public function index()
    {
        $this->assign('title', '会员管理');
        return $this->fetch();
    }

    /***
     * 输出用户数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ajax()
    {
        $request = Request::instance();
        if (!$request->isAjax()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }
        $user = new User();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;

        $total = $user->count('userid');
        $data = $user->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }

    /***
     * 添加用户
     * @return array|mixed
     */
    public function adduser()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data = $request->param();
            $validate = Loader::validate('AddUserValidate');
            if (!$validate->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }


            $user = User::get(['username' => $request->param('username')]);
            if (!is_null($user)) {
                return ['code'=>-1,'msg'=>'账号已存在'];
            }


            User::create([
                'username' => $data['username'],
                'password' => md5($data['password']),
                'usernick' => $data['usernick'],
                'userstat' => $data['userstats'],

            ]);
            return ['code'=>1,'msg'=>'添加成功'];
        }
        return $this->fetch();
    }

    /***
     * 删除用户
     * @return array
     */
    public function dell()
    {
        $request = Request::instance();
        $userid = $request->has('userid')?$request->param('userid'):false;
        if (!$userid) {
            return ['code' => -1, 'msg' => '参数错误'];
        }

        if (User::destroy($userid)) {
            return ['code' => 1, 'msg' => '删除成功'];
        } else {
            return ['code' => -1, 'msg' => '删除失败'];
        }
    }

    /***
     * 编辑用户
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $request = Request::instance();
        if ($request->isAjax()) {
            $data = $request->param();
            $validate = Loader::validate('AddUserValidate');
            if (!$validate->scene('edit')->check($data)) {
                return ['code'=>-1,'msg'=>$validate->getError()];
            }
            $uid= User::where('username',$data['username'])->find();

            if ($uid->userid=='1' && $data['userstats']!='1') {
                return ['code'=>-1,'msg'=>'超级管理员权限不能更改'];
            }


            if (empty($data['password'])) {
                User::where('username', $data['username'])->update([
                    'usernick' => $data['usernick'],
                    'userstat' => $data['userstats'],
                ]);
            }else{
                User::where('username', $data['username'])->update([
                    'usernick' => $data['usernick'],
                    'password' => md5($data['password']) ,
                    'userstat' => $data['userstats'],
                ]);
            }
            return ['code'=>1,'msg'=>'修改成功'];
        }
        $userinfo= User::get($request->param('userid'));
        $this->assign('username', $userinfo->username);
        $this->assign('usernick', $userinfo->usernick);
        $this->assign('userstat', $userinfo->userstat);
        return $this->fetch();
    }



}